package com.yimi.policyservice.controller;

import com.github.pagehelper.PageHelper;
import com.yimi.policyservice.common.annotation.Auth;
import com.yimi.policyservice.common.constant.GloabConstant;
import com.yimi.policyservice.dto.PageDto;
import com.yimi.policyservice.dto.enums.FunctionCodeEnum;
import com.yimi.policyservice.dto.policyaddr.IdsBean;
import com.yimi.policyservice.dto.policycomminsure.PolicyComminsureBean;
import com.yimi.policyservice.dto.policycomminsure.PolicyComminsureItemDto;
import com.yimi.policyservice.dto.policycomminsure.PolicyComminsureItemPriceDto;
import com.yimi.policyservice.dto.policycomminsure.PolicyComminsureProductDto;
import com.yimi.policyservice.entity.base.policy.PolicyComminsureItem;
import com.yimi.policyservice.entity.base.policy.PolicyComminsureProduct;
import com.yimi.policyservice.entity.base.system.SysUser;
import com.yimi.policyservice.entity.base.system.SysUserDataPermission;
import com.yimi.policyservice.param.product.ComminsureParam;
import com.yimi.policyservice.service.PolicyComminsureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 商保方案
 * @author jack
 */
@RestController
@RequestMapping(value = "insure")
public class PolicyComminsureController extends  BaseController {

    @Autowired
    private PolicyComminsureService policyComminsureService ;

    /**
     * 创建
     * @param bean
     * @return
     */
    @RequestMapping(value = "", method =  RequestMethod.POST)
    @Auth(code = FunctionCodeEnum.INSURE_CREATE)
    public PolicyComminsureProduct create(@RequestBody PolicyComminsureBean bean){
        return policyComminsureService.create(bean,getLoginUser());
    }


    /**
     * 删除
     * @param bean
     */
    @RequestMapping(value = "del", method =  RequestMethod.POST)
    @Auth(code = FunctionCodeEnum.INSURE_DELETE)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void delete(@RequestBody IdsBean bean){
         policyComminsureService.delete(bean.getIds());
    }


    /**
     * 搜索
     * @param companyId
     * @param productId
     */
    @RequestMapping(value = "search", method =  RequestMethod.POST)
    @Auth(code = FunctionCodeEnum.INSURE_LIST)
    public Object search(@RequestBody ComminsureParam comminsureParam)
    {
        handlerPage(comminsureParam);
        comminsureParam.setHasDatapermission(true);
        SysUserDataPermission permission = new SysUserDataPermission();
        permission.setUserId(userId());
        comminsureParam.setUserDataPermission(permission);
        return new PageDto<>( policyComminsureService.search(comminsureParam));
    }


    /**
     * 修改
     * @param bean
     * @param id
     */
    @RequestMapping(value = "{id}", method =  RequestMethod.PUT)
    @Auth(code = FunctionCodeEnum.INSURE_DELETE)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void update(@RequestBody PolicyComminsureBean bean,@PathVariable String id){
        policyComminsureService.update(bean,id,getLoginUser());
    }


    /**
     * 详情
     * @param id
     */
    @RequestMapping(value = "{id}", method =  RequestMethod.GET)
    @Auth(code = FunctionCodeEnum.INSURE_DELETE)
    public PolicyComminsureBean findOne(@PathVariable String id){
        return policyComminsureService.findOne(id);
    }

    /**
     * 获取所有商保方案
     * @return
     */
    @RequestMapping(value = "all", method =  RequestMethod.GET)
    public List<PolicyComminsureProductDto> findAll(){
        return  policyComminsureService.getAll() ;
    }
    /**
     * 获取所有商保方案
     * @return
     */
    @RequestMapping(value = "allByPermission", method =  RequestMethod.GET)
    public List<PolicyComminsureProductDto> findAllByPermission(){
        return  policyComminsureService.getAllByPermission(userId()) ;
    }

    /**
     * 获取方案下项目列表
     * @return
     */
    @RequestMapping(value = "{id}/items",method = RequestMethod.GET)
    public List<PolicyComminsureItemDto> getProductItems(@PathVariable String id){
        return policyComminsureService.findByproductId(id);
    }

    @RequestMapping(value = "{id}/price",method = RequestMethod.POST)
    @Auth
    public void  setProductPrice(@RequestBody List<PolicyComminsureItemPriceDto>bean,@PathVariable String id){
        SysUser sysUser = getLoginUser();
        bean.forEach(item ->{
            item.setCreateId(sysUser.getId());
            item.setProductId(id);
            item.setUpdateId(sysUser.getId());
            item.setUpdateTime(new Date());
            item.setCreateTime(new Date());
        });
        policyComminsureService.setProice(bean,id);
    }

    @RequestMapping(value = "{id}/price",method = RequestMethod.GET)
    @Auth
    public List<PolicyComminsureItemPriceDto>  getProductPrice(@PathVariable String id){
        return policyComminsureService.getProice(id);
    }



}
